﻿Public Class frmEntryFormula
    Dim dbSystem As New dbSystem
    Dim dbBase As New dbFormula
    Dim Var As New varDeclare
    Dim frmCtrl As New FormControl
    Dim curr2word As New Currency
    Dim dsBase As DataSet
    Dim dtItemDetail As DataTable
    Dim TotalDebet As Decimal
    Dim TotalCredit As Decimal
    Public EntryID As String
    Dim dgvTemp As New DataGridView
    Dim TypeAct As String

    Private Sub frmEntryFormula_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgvTemp.Columns.Add("clID", "clID")
        dgvTemp.Rows.Add(EntryID)
        dgvTemp.Rows(0).Selected = True
        ToolbarHeader1.DefaultRow = 1
        ToolbarHeader1.Iniliaze("Entry : Formula Production", Me, New frmListFormula, dgvTemp, "clID")
        Dim cmbItem As New List(Of comboItem)
        cmbItem.Add(New comboItem("Active", "A"))
        cmbItem.Add(New comboItem("Inactive", "I"))
        cmbFormulaStatus.DataSource = cmbItem
        cmbFormulaStatus.DisplayMember = "Text"
        cmbFormulaStatus.ValueMember = "Value"
        dgSummary.Rows.Add(1)
        dgSummary.Rows(0).Cells(0).Value = "Add Line"
        dgSummary.Rows(0).Cells(1).Value = "0.00"
        dgSummary.Rows(0).Cells(5).Value = "0.00"
        dtItemDetail = dbBase.GetItemDetailList().Tables(0)
        If (EntryID <> "") Then
            TypeAct = "U"
            ToolbarHeader1.ActionCtrl(True, False, False, False, True, True, False, True, True)
        Else
            TypeAct = "I"
            ToolbarHeader1.ActionCtrl(False, False, False, False, False, True, False, False, True)
        End If

    End Sub

    Private Function SaveValidation() As Boolean
        Dim status As Boolean = False
        Select Case TypeAct
            Case "D"
                If (EntryID <> "") Then
                    status = True
                End If
            Case "I"

            Case "U"

        End Select
        Return status
    End Function

    ''================================== Control Header ==============================================''
    Private Sub tsNew_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsNewClick
        Dim OpenForm As New frmEntryFormula
        OpenForm.EntryID = ""
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsEditClick

    End Sub

    Private Sub tsDelete_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            Dim result As String = "" 'dbGL.deleteJournalGL(txtJournalID.Text, Var.CompID)
            frmCtrl.openChild(New frmListFormula, Me.ParentForm)
        Else : MessageBox.Show("Formula is Not Exist")
        End If
    End Sub

    Private Sub tsSave_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsSaveClick
        If (SaveValidation()) Then
            TypeAct = "I"
            Dim status As String = ""
            If (status.IndexOf("Success") > 0) Then
                If (status.Length > 7) Then
                    MessageBox.Show(status)
                End If
                frmCtrl.openChild(New frmListFormula, Me.ParentForm)
            Else
                MessageBox.Show("Saving Price Calculation is Failed")
            End If
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsRefreshClick
        Dim OpenForm As New frmEntryFormula
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsCancelClick
        Dim OpenForm As New frmEntryFormula
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub
    ''================================== Control Header [End] ==============================================''

    Private Sub dgSummary_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgSummary.CellContentClick
        If (e.RowIndex = 0 And e.ColumnIndex = 0) Then
            Dim status As Boolean = False
            If (dgvItem.Rows.Count > 0) Then
                If (dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clGroups").Value <> "") Then
                    status = True
                End If
            Else
                status = True
            End If
            If (status) Then
                dgvItem.Rows.Add(1)
                dgvItem.Item(0, dgvItem.Rows.Count - 1).Value = dgvItem.Rows.Count
            End If
            Dim formFind As New frmFind
            formFind.lblTitle.Text = "Search : Formula Item"
            formFind.QueryHeader = "SELECT IC.GROUPS 'Groups', IC.Code 'Item Code', IC.IC_DESCRIPTION 'Item Desc',IC. STATUS 'Status' FROM ITEM_CODE IC WITH(NOLOCK) INNER JOIN GROUP_ITEM GI WITH(NOLOCK) ON GI.GROUPS = IC.GROUPS WHERE ISNULL(IC.COMP, '" + Var.CompID + "') = '" + Var.CompID + "'"
            formFind.StartPosition = FormStartPosition.CenterScreen
            formFind.type = "cell"
            formFind.dgCellValue1 = dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clGroups")
            formFind.dgCellValue2 = dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clCode")
            formFind.Show()
            MDI.Enabled = False
        End If
    End Sub

    Private Sub dgvItem_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvItem.CellValueChanged
        If (dgvItem.Rows.Count > 0) Then
            If (dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clGroups").Value <> "") Then
                Dim dvFind As DataView = New DataView(dtItemDetail)
                dvFind.RowFilter = "[Code] = '" & dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clGroups").Value & "-" & dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clCode").Value & "'"
                If (dvFind.Count > 0) Then
                    dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clDescription").Value = dvFind.Item(0).Row("Name").ToString()
                    dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("cUM").Value = dvFind.Item(0).Row("UM").ToString()
                    dgvItem.Rows(dgvItem.Rows.Count - 1).Cells("clPrice").Value = dvFind.Item(0).Row("Price List").ToString()
                End If
            End If
            If (e.RowIndex > -1) Then
                If (IsNumeric(dgvItem.Rows(e.RowIndex).Cells("clQty").Value)) Then
                    dgSummary.Rows(0).Cells(1).Value = curr2word.dec2curr(sumQty(0))
                    If (IsNumeric(dgvItem.Rows(e.RowIndex).Cells("clPrice").Value)) Then
                        Dim Amount As String = curr2word.dec2curr((CDec(dgvItem.Rows(e.RowIndex).Cells("clPrice").Value) * CDec(dgvItem.Rows(e.RowIndex).Cells("clQty").Value)))
                        dgvItem.Rows(e.RowIndex).Cells("clAmount").Value = Amount
                    End If
                    dgvItem.Rows(e.RowIndex).Cells("clQty").Value = curr2word.dec2curr(dgvItem.Rows(e.RowIndex).Cells("clQty").Value)
                    dgSummary.Rows(0).Cells(2).Value = curr2word.dec2curr(sumAmount(0))
                    recalcPercent()
                End If
            End If
        End If
    End Sub

    Private Sub recalcPercent()
        For i = 0 To dgvItem.Rows.Count - 1
            If (IsNumeric(dgvItem.Rows(i).Cells("clQty").Value)) Then
                dgvItem.Rows(i).Cells("clPrcQty").Value = curr2word.dec2curr((CDec(dgvItem.Rows(i).Cells("clQty").Value) / CDec(dgSummary.Rows(0).Cells(1).Value)) * 100)
            End If
        Next
    End Sub

    Private Function sumQty(sum As Decimal) As Decimal
        For i = 0 To dgvItem.Rows.Count - 1
            If (IsNumeric(dgvItem.Rows(i).Cells("clQty").Value)) Then
                sum += CDec(dgvItem.Rows(i).Cells("clQty").Value)
            End If
        Next
        Return sum
    End Function

    Private Function sumAmount(sum As Decimal) As Decimal
        For i = 0 To dgvItem.Rows.Count - 1
            If (IsNumeric(dgvItem.Rows(i).Cells("clAmount").Value)) Then
                sum += CDec(dgvItem.Rows(i).Cells("clAmount").Value)
            End If
        Next
        Return sum
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnAddSteps.Click
        Dim status As Boolean = False
        If (dgSteps.Rows.Count > 0) Then
            If (dgSteps.Rows(dgSteps.Rows.Count - 1).Cells(1).Value <> "") Then
                status = True
            End If
        Else
            status = True
        End If
        If (status) Then
            dgSteps.Rows.Add(1)
            dgSteps.Item(0, dgSteps.Rows.Count - 1).Value = dgSteps.Rows.Count
        End If
    End Sub

    Private Sub txtGroups_DoubleClick(sender As Object, e As EventArgs) Handles txtGroups.DoubleClick
        Dim formFind As New frmFind
        formFind.lblTitle.Text = "Search : Groups"
        formFind.QueryHeader = "SELECT GROUPS 'Groups ID', GROUP_DESCRIPTION 'Group Desc', GROUP_STATUS_CODE 'Status' FROM GROUP_ITEM WITH(NOLOCK) WHERE ISNULL(COMP, '" + Var.CompID + "') = '" + Var.CompID + "'"
        formFind.StartPosition = FormStartPosition.CenterScreen
        formFind.type = "txt"
        formFind.txtValue = txtGroups
        formFind.Show()
        MDI.Enabled = False
    End Sub

    Private Sub txtCode_DoubleClick(sender As Object, e As EventArgs) Handles txtCode.DoubleClick
        If (txtGroups.Text <> "") Then
            Dim formFind As New frmFind
            formFind.lblTitle.Text = "Search : Code"
            Dim groups As String = "NULL"
            If (txtGroups.Text <> "") Then
                groups = txtGroups.Text
            End If
            formFind.QueryHeader = "SELECT IC.Code 'Item Code', IC.IC_DESCRIPTION 'Item Desc',IC. STATUS 'Status' FROM ITEM_CODE IC WITH(NOLOCK) INNER JOIN GROUP_ITEM GI WITH(NOLOCK) ON GI.GROUPS = IC.GROUPS WHERE ISNULL(IC.COMP, '" + Var.CompID + "') = '" + Var.CompID + "' AND IC.GROUPS = ISNULL(" + groups + ",IC.GROUPS)"
            formFind.StartPosition = FormStartPosition.CenterScreen
            formFind.type = "txt"
            formFind.txtValue = txtCode
            formFind.Show()
            MDI.Enabled = False
        End If
    End Sub

    Private Sub txtCode_TextChanged(sender As Object, e As EventArgs) Handles txtCode.TextChanged
        If (txtCode.Text <> "") Then
            Dim dvFind As DataView = New DataView(dtItemDetail)
            dvFind.RowFilter = "[Code] = '" & txtGroups.Text & "-" & txtCode.Text & "'"
            If (dvFind.Count > 0) Then
                txtFormulaDescription.Text = dvFind.Item(0).Row("Name").ToString()
                txtFormulaUM.Text = dvFind.Item(0).Row("UM").ToString()
            End If
        End If
    End Sub
End Class